package com.qijian.flowable.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.qijian.flowable.domain.vo.MatrixFieldVo;
import lombok.RequiredArgsConstructor;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.enums.BusinessType;
import com.qijian.flowable.domain.MatrixFieldVal;
import com.qijian.flowable.domain.query.MatrixFieldValQuery;
import com.qijian.flowable.service.IMatrixFieldValService;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 流程矩阵字段值Controller
 *
 * @author qijian
 * @date 2022-02-08
 */
@Api(tags = "流程矩阵字段值")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowable/matrixFieldVal")
public class MatrixFieldValController extends BaseController {

    private final IMatrixFieldValService matrixFieldValService;

    /**
     * 查询流程矩阵字段值列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:list')")
    @GetMapping("/list")
    public TableDataInfo list(MatrixFieldValQuery query, IPage<MatrixFieldVal> page) {
        return getDataTable(matrixFieldValService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出流程矩阵字段值列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:export')")
    @Log(title = "流程矩阵字段值", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(MatrixFieldValQuery query, HttpServletResponse response) {
//        new ExcelUtil<>(MatrixFieldVal.class).exportExcel(query), "流程矩阵字段值数据");
        new ExcelUtil<>(MatrixFieldVo.class).exportExcel(response, matrixFieldValService.export(query), "流程矩阵字段值数据");
    }

    /**
     * 获取流程矩阵字段值详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:query')")
    @GetMapping(value = "/{fieldValId}")
    public AjaxResult getInfo(@PathVariable("fieldValId") Long fieldValId) {
        return AjaxResult.success(matrixFieldValService.getById(fieldValId));
    }

    /**
     * 新增流程矩阵字段值
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:add')")
    @Log(title = "流程矩阵字段值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<MatrixFieldVal> matrixFieldValList) {
        return toAjax(matrixFieldValService.saveBatch(matrixFieldValList));
    }

    /**
     * 修改流程矩阵字段值
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:edit')")
    @Log(title = "流程矩阵字段值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<MatrixFieldVal> matrixFieldValList) {
        matrixFieldValService.saveOrUpdateMatrixFieldVal(matrixFieldValList);
        return AjaxResult.success();
    }

    /**
     * 删除流程矩阵字段值
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:remove')")
    @Log(title = "流程矩阵字段值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fieldValIds}")
    public AjaxResult remove(@PathVariable List<Long> fieldValIds) {
        return toAjax(matrixFieldValService.removeByIds(fieldValIds));
    }
}
